home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / What's New? / Development Kits / Apple Game Sprockets DR1 / Examples / SoundSprocketTest / TS3Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-24  |  5.1 KB  |  272 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        TS3Menu.c
  3.  *    Author:        Dan Venolia
  4.  *
  5.  *    Copyright © 1996 Apple Computer, Inc.
  6.  */
  7.  
  8. #include <assert.h>
  9.  
  10. #include <Dialogs.h>
  11. #include <Menus.h>
  12. #include <Resources.h>
  13. #include <TextUtils.h>
  14.  
  15. #include "TS3Main.h"
  16. #include "TS3Menu.h"
  17. #include "TS3Resource.h"
  18. #include "TS3Sound.h"
  19. #include "TS3TestAPI.h"
  20.  
  21.  
  22. typedef struct Node Node;
  23. struct Node {
  24.     Node*            lo;
  25.     Node*            hi;
  26.     Str255            name;
  27. };
  28.  
  29.  
  30. static MenuHandle    gMenuSound            = NULL;
  31. static short        gMenuSoundItem        = kSoundItem_Silence;
  32.  
  33.  
  34. static void Insert(
  35.     Node**            ioRoot,
  36.     Node*            inNode);
  37.  
  38. static void Traverse(
  39.     Node*            inNode);
  40.  
  41. static void SelectAppleMenu(
  42.     short            inItem);
  43.  
  44. static void SelectFileMenu(
  45.     short            inItem);
  46.  
  47. static void SelectSoundMenu(
  48.     short            inItem);
  49.  
  50.  
  51. /* =============================================================================
  52.  *        Menu_Init (external)
  53.  *
  54.  *    Initializes our menus.
  55.  * ========================================================================== */
  56. void Menu_Init(
  57.     void)
  58. {
  59.     Node*            root;
  60.     Node*            node;
  61.     short            count;
  62.     short            index;
  63.     Handle            resource;
  64.     short            resID;
  65.     ResType            resType;
  66.     Str255            resName;
  67.     
  68.     SetMenuBar(GetNewMBar(kMBarID_Main));
  69.     DrawMenuBar();
  70.     
  71.     AppendResMenu(GetMenuHandle(kMenuID_Apple), 'DRVR');
  72.     
  73.     // Build the sound menu
  74.     gMenuSound = GetMenuHandle(kMenuID_Sound);
  75.     
  76.     root = NULL;
  77.     
  78.     SetResLoad(false);
  79.     count = Count1Resources('snd ');
  80.     for (index = 1; index <= count; index++)
  81.     {
  82.         // Grab the resource name
  83.         resource = Get1IndResource('snd ', index);
  84.         assert(ResError() == noErr);
  85.         assert(resource != NULL);
  86.         
  87.         GetResInfo(resource, &resID, &resType, resName);
  88.         
  89.         ReleaseResource(resource);
  90.         
  91.         // Insert a new node in the tree
  92.         node = (Node*) NewPtr(sizeof(Node));
  93.         assert(node != NULL);
  94.         
  95.         node->lo = NULL;
  96.         node->hi = NULL;
  97.         BlockMove(resName, node->name, resName[0]+1);
  98.         
  99.         Insert(&root, node);
  100.     }
  101.     SetResLoad(true);
  102.     
  103.     Traverse(root);
  104. }
  105.  
  106.  
  107. /* =============================================================================
  108.  *        Insert (internal)
  109.  *
  110.  *    Sorts the given node into the tree.
  111.  * ========================================================================== */
  112. void Insert(
  113.     Node**            ioRoot,
  114.     Node*            inNode)
  115. {
  116.     if (*ioRoot != NULL)
  117.     {
  118.         if (CompareString((*ioRoot)->name, inNode->name, NULL) > 0)
  119.         {
  120.             Insert(&(*ioRoot)->lo, inNode);
  121.         }
  122.         else
  123.         {
  124.             Insert(&(*ioRoot)->hi, inNode);
  125.         }
  126.     }
  127.     else
  128.     {
  129.         *ioRoot = inNode;
  130.     }
  131. }
  132.  
  133.  
  134. /* =============================================================================
  135.  *        Traverse (internal)
  136.  *
  137.  *    Appends the tree in alpha order onto the sound menu, and kills the tree.
  138.  * ========================================================================== */
  139. void Traverse(
  140.     Node*            inNode)
  141. {
  142.     if (inNode != NULL)
  143.     {
  144.         Traverse(inNode->lo);
  145.         
  146.         AppendMenu(gMenuSound, inNode->name);
  147.         
  148.         Traverse(inNode->hi);
  149.         
  150.         DisposePtr((Ptr) inNode);
  151.     }
  152. }
  153.  
  154.  
  155. /* =============================================================================
  156.  *        Menu_Exit (external)
  157.  *
  158.  *    Cleans up.
  159.  * ========================================================================== */
  160. void Menu_Exit(
  161.     void)
  162. {
  163. }
  164.  
  165.  
  166. /* =============================================================================
  167.  *        Menu_Select (external)
  168.  *
  169.  *    Takes action on the given menu item.
  170.  * ========================================================================== */
  171. void Menu_Select(
  172.     short            inMenuID,
  173.     short            inItem)
  174. {
  175.     switch (inMenuID)
  176.     {
  177.         case kMenuID_Apple:
  178.             SelectAppleMenu(inItem);
  179.         break;
  180.         
  181.         case kMenuID_File:
  182.             SelectFileMenu(inItem);
  183.         break;
  184.         
  185.         case kMenuID_Sound:
  186.             SelectSoundMenu(inItem);
  187.         break;
  188.     }
  189.  
  190.     HiliteMenu(0);
  191. }
  192.  
  193.  
  194. /* =============================================================================
  195.  *        SelectAppleMenu (internal)
  196.  *
  197.  *    Takes action on the given Apple menu item.
  198.  * ========================================================================== */
  199. void SelectAppleMenu(
  200.     short            inItem)
  201. {
  202.     switch (inItem)
  203.     {
  204.         case kAppleItem_About:
  205.             Alert(kAlrtID_About, NULL);
  206.         break;
  207.     }
  208. }
  209.  
  210.  
  211. /* =============================================================================
  212.  *        SelectFileMenu (internal)
  213.  *
  214.  *    Takes action on the given File menu item.
  215.  * ========================================================================== */
  216. void SelectFileMenu(
  217.     short            inItem)
  218. {
  219.     switch (inItem)
  220.     {
  221.         case kFileItem_RunQuiet:
  222.             TestAPI_Execute();
  223.         break;
  224.         
  225.         case kFileItem_Config3DSound:
  226.             Sound_Configure();
  227.         break;
  228.         
  229.         case kFileItem_Quit:
  230.             Main_LastRoundup();
  231.         break;
  232.     }
  233. }
  234.  
  235.  
  236. /* =============================================================================
  237.  *        SelectSoundMenu (internal)
  238.  *
  239.  *    Takes action on the given Sound menu item.
  240.  * ========================================================================== */
  241. void SelectSoundMenu(
  242.     short            inItem)
  243. {
  244.     Str255            name;
  245.     
  246.     CheckItem(gMenuSound, gMenuSoundItem, false);
  247.     
  248.     switch (inItem)
  249.     {
  250.         case kSoundItem_Silence:
  251.             Sound_PlaySilence();
  252.             gMenuSoundItem = kSoundItem_Silence;
  253.         break;
  254.         
  255.         default:
  256.             GetMenuItemText(GetMenuHandle(kMenuID_Sound), inItem, name);
  257.             
  258.             if (Sound_PlayResource(name))
  259.             {
  260.                 gMenuSoundItem = inItem;
  261.             }
  262.             else
  263.             {
  264.                 gMenuSoundItem = kSoundItem_Silence;
  265.             }
  266.     }
  267.     
  268.     CheckItem(gMenuSound, gMenuSoundItem, true);
  269. }
  270.  
  271.  
  272.